package com.ocom.common.utils;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * ZIP操作工具类
 * @说明 注意引入ant.jar文件
 * @author cuisuqiang
 * @version 1.0
 * @since
 */
public class ZipUtils {
	/**
	 * 解压到指定目录
	 */
	public static void unZipFiles(String zipPath,String descDir)throws IOException
	{
		unZipFiles(new File(zipPath), descDir);
	}
	/**
	 * 解压文件到指定目录
	 */
	@SuppressWarnings("rawtypes")
	public static List<String> unZipFiles(File zipFile, String descDir)throws IOException
	{
		File pathFile = new File(descDir);
		if(!pathFile.exists())
		{
			pathFile.mkdirs();
		}
		//解决zip文件中有中文目录或者中文文件
		ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));

		List<String> stringList=new ArrayList<>();

		Integer number= 1;

		for(Enumeration entries = zip.entries(); entries.hasMoreElements();)
		{
			number++;
			ZipEntry entry = (ZipEntry)entries.nextElement();
			String zipEntryName = entry.getName();
			InputStream in = zip.getInputStream(entry);
			String outPath = (descDir+zipEntryName).replaceAll("\\*", "/");;
			//判断路径是否存在,不存在则创建文件路径
			File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
			if(!file.exists())
			{
				file.mkdirs();
			}
			//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
			if(new File(outPath).isDirectory())
			{
//                List<String> inner =  getFilePath(outPath);
//                stringList.addAll(inner);
				continue;
			}
			//输出文件路径信息
			System.out.println(outPath);

			OutputStream out = new FileOutputStream(outPath);
			byte[] buf1 = new byte[1024];
			int len;
			while((len=in.read(buf1))>0)
			{
				out.write(buf1,0,len);
			}
			in.close();
			out.close();

			String newPath = descDir+"/"+number+"/" ; //+new File(outPath).getName()
			File newDirPath= new File(newPath);
			if(!newDirPath.exists())
			{
				newDirPath.mkdirs();
			}
			File olfFile = new File(outPath);
			copyFileUsingFileChannels(new File(outPath),new File(newPath+new File(outPath).getName()));
			stringList.add(newPath+new File(outPath).getName());
			olfFile.delete();
		}
		zip.close();
		System.out.println("******************解压完毕********************");
		return stringList;
	}
	public static void main(String[] args) throws IOException {
		/**
		 * 解压文件
		 */
		File zipFile = new File("E:/test/123.zip");
		String path = "E:/test/0501bea57d4a/";
		unZipFiles(zipFile, path);
	}

	/**
	 * 获取文件夹下所有文件的路径
	 *
	 * @param folderPath
	 * @return
	 */
	public static List<String> getFilePath(String folderPath) {
		File folder = new File(folderPath);
		List<String> filePathList = new ArrayList<>();
		String rootPath;
		if (folder.exists()) {
			String[] fileNameList = folder.list();
			if (null != fileNameList && fileNameList.length > 0) {
				if (folder.getPath().endsWith(File.separator)) {
					rootPath = folder.getPath();
				} else {
					rootPath = folder.getPath() + File.separator;
				}
				for (String fileName : fileNameList) {
					String newPath= rootPath + fileName;
					filePathList.add(newPath);
				}
			}
		}
		return filePathList;
	}


	private static void copyFileUsingFileChannels(File source, File dest) throws IOException {
		FileChannel inputChannel = null;
		FileChannel outputChannel = null;
		try {
			inputChannel = new FileInputStream(source).getChannel();
			outputChannel = new FileOutputStream(dest).getChannel();
			outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
		} finally {
			inputChannel.close();
			outputChannel.close();
		}
	}
}